﻿using System;
using T.FAS.Runtime.DataAccess.Engine.ValidateChecker.Entity;

namespace T.FAS.Runtime.DataAccess.Engine
{

    public class DateTimeValidateFactory : CommonFactory
    {
        public override IColumnValidater GetDataTypeValidater()
        {
            return new DateTimeDataTypeValidater();
        }
        public override IColumnValidater GetLengthValidater()
        {
            return new DateTimeDataTypeValidater();
        }

    }

    public class DateTimeDataTypeValidater : IColumnDataTypeValidater
    {
        public ValidateCheckItem Validate(ValidateCheckItem item)
        {
            if (item.Result.IsStop)
                return item;
            //数据类型验证
            if (item.InputValue is DateTime dateTime)
            {
                item.ConvertValue = dateTime;
            }
            else
            {
                var inputVal = item.InputValue.ToString();
                if (!DateTime.TryParse(inputVal, out DateTime value))
                {
                    item.Result.Flag = false;
                    item.Result.IsStop = true;
                    item.Result.Msg.Append(string.Format(ValidateConst.DataTypeErrorTemp, item.CheckElement.Name, inputVal, item.CheckElement.DataType));
                }
                else
                {
                    item.ConvertValue = value;
                }
            }
            return item;
        }
    }

    public class DateTimeLengthValidater : IColumnLengthValidater
    {
        /// <summary>
        /// Boolean默认不用校验长度
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public ValidateCheckItem Validate(ValidateCheckItem item)
        {
            return item;
        }
    }
}
